package com.mibao.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import com.mibao.adapter.ContactsAdapter;
import com.mipai.R;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by tianweishu on 2014/10/27.
 */
public class ContactsActivity extends Activity  implements Serializable  {

	private static final long serialVersionUID = 1L;
	private List<ContactsAdapter.ConatctListItem> conatctListItems;
    private ContactsAdapter adapter;

    private ListView listView;
    
	private SharedPreferences preferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.contacts_layout);

        conatctListItems = new ArrayList<ContactsAdapter.ConatctListItem>();
        adapter = new ContactsAdapter(this, conatctListItems);
        listView = (ListView) findViewById(R.id.contacts_list);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ContactsAdapter.ConatctListItem conatctListItem = conatctListItems.get(position);
                Intent i = new Intent(ContactsActivity.this, ContactsDetail.class);
                i.putExtra("contact", (Serializable)conatctListItem);
                startActivity(i);
            }
        });
        refresh();
        
        ////////XYC////////
        TelephonyManager mTelephonyMgr = (TelephonyManager)
        getSystemService(Context.TELEPHONY_SERVICE); 
        String myPhoneNum = mTelephonyMgr.getLine1Number();
        preferences = getSharedPreferences("config", MODE_PRIVATE);
        Editor editor = preferences.edit();
		editor.putString("myPhoneNum", myPhoneNum);
		editor.commit();
        //////////////////
    }

    private void initTitle(){
        ((TextView)findViewById(R.id.head_text)).setText("密友");
//        findViewById(R.id.head_left_img).setBackgroundResource(R.drawable.);
    }
    private void refresh(){
        for (int i = 0; i < 5; i++) {
            ContactsAdapter.ConatctListItem item = new ContactsAdapter.ConatctListItem();
            item.setName("李达奇" + i);
            item.setNumber("123456789" + i);
            item.setUnreadPhone(i % 2 == 0? 1: 0);
            conatctListItems.add(item);
        }
        adapter.notifyDataSetChanged();
    }
}
